package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.service.GoodsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goodsController")
public class GoodsController {

    @DubboReference
    private GoodsService goodsService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //查询全部商品信息
    @RequestMapping("getGoods")
    @ResponseBody
    public AjaxResult<List<TbGoods>> getGoods(String goodsName){
        if ( goodsName == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            List<TbGoods> goodsList = goodsService.getGoodsByName(goodsName);
            return AjaxResult.OK().setData(goodsList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }


    //审核通过
    @RequestMapping("auditOk")
    @ResponseBody
    public AjaxResult auditOk(Long [] ids,String auditStatus){
        if (ids.length == 0 && auditStatus == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            goodsService.auditOk(ids,auditStatus);
            rocketMQTemplate.convertAndSend("item-topic",ids);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
    //驳回
    @RequestMapping("auditNo")
    @ResponseBody
    public AjaxResult auditNo(Long [] ids,String auditStatus){
        if (ids.length == 0 && auditStatus == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            goodsService.auditNo(ids,auditStatus);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //删除信息
    @RequestMapping("deleteGoods")
    @ResponseBody
    public AjaxResult deleteGoods(Long [] ids){
        if (ids.length == 0){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            goodsService.deleteGoods(ids);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
